defmy_func(a,b,c):print("a={0}, b={1}, c={2}".format(a,b,c))
my_func(1,2,3)
a=1, b=2, c=3
defmy_func(a,b=2,c=3):print("a={0}, b={1}, c={2}".format(a,b,c))
Note that once a parameter is assigned a default value, all parameters thereafter must be asigned a default value too!
For example, this will not work:
deffn(a,b=2,c):print(a,b,c)
File "<ipython-input-4-2180ec769037>", line 1 def fn(a, b=2, c): ^SyntaxError: non-default argument follows default argument
defmy_func(a,b=2,c=3):print("a={0}, b={1}, c={2}".format(a,b,c))
my_func(10,20,30)
a=10, b=20, c=30
my_func(10,20)
a=10, b=20, c=3
my_func(10)
a=10, b=2, c=3
Since a does not have a default value, it must be specified:
my_func()
---------------------------------------------------------------------------TypeError Traceback (most recent call last) <ipython-input-9-d82eda95de40> in <module>()----> 1my_func()TypeError: my_func() missing 1 required positional argument: 'a'
Positional arguments, can optionally, be specified using their corresponding parameter name.
This allows us to pass the arguments without using the positional assignment:
defmy_func(a,b=2,c=3):print("a={0}, b={1}, c={2}".format(a,b,c))
my_func(c=30,b=20,a=10)
a=10, b=20, c=30
my_func(10,c=30,b=20)
a=10, b=20, c=30
Note that once a keyword argument has been used, all arguments thereafter must also be named:
my_func(10,b=20,30)
File "<ipython-input-13-ea05eeab2151>", line 1 my_func(10, b=20, 30) ^SyntaxError: positional argument follows keyword argument
However, if a parameter has a default value, it can be omitted from the argument list, named or not:
my_func(10,c=30)
a=10, b=2, c=30
my_func(a=30,c=10)
a=30, b=2, c=10
my_func(c=10,a=30)
a=30, b=2, c=10